<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane label="角色管理" name="first">
            <!-- 按钮 -->
            <el-button type="primary" size="mini" @click="addRole">+ 新增角色</el-button>

            <!-- 表格 -->
            <el-table
              :data="tableData"
              style="width: 100%"
            >

              <!-- 序号 -->
              <el-table-column
                label="序号"
                width="50"
              >
                <template slot-scope="scope">
                  <span style="margin-left: 10px">{{ (roleList.page-1)*roleList.pagesize+scope.$index+1 }}</span>
                </template>
              </el-table-column>

              <!-- 角色 -->
              <el-table-column
                label="角色名称"
                prop="name"
                style="width:100%"
              />

              <!-- 描述 -->
              <el-table-column
                label="描述"
                style="width:100%"
                prop="description"
              />

              <!-- 操作 -->
              <el-table-column label="操作" style="width:100%;">
                <template slot-scope="scope">
                  <el-button
                    type="success"
                    size="mini"
                    @click="handleEdit(scope.$index, scope.row)"
                  >分配权限</el-button>
                  <el-button
                    type="primary"
                    size="mini"
                    @click="editHandler(scope.row)"
                  >编辑</el-button>
                  <el-button
                    size="mini"
                    type="danger"
                    @click="handleDelete(scope.$index,scope.row)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <!-- 分页 -->
            <el-pagination
              name="first"
              :page-sizes="[2,5,10]"
              :page-size="roleList.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="second">
            <!-- 表单 -->
            <el-form ref="companyForm" :model="companyForm" label-width="100px" style="width:450px">
              <el-form-item label="企业名称">
                <el-input v-model="companyForm.name" disabled />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyForm.companyAddress" disabled />
              </el-form-item>
              <el-form-item label="公司电话">
                <el-input v-model="companyForm.companyPhone" disabled />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyForm.mailbox" disabled />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyForm.remarks" disabled />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>

        <!-- 添加角色对话框 -->
        <el-dialog
          title="提示"
          :visible.sync="dialogVisible"
          width="40%"
          :before-close="handleClose"
        >
          <setDialog
            ref="set"
            :row="row"
            :is-edit="isEdit"
            @success="success"
          />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import setDialog from '@/views/settings/setDialog'

import { getRole, deleteRole, getCompanyInfo } from '@/api/setting'
export default {
  components: {
    setDialog
  },
  data() {
    return {
      activeName: 'first',
      tableData: [],
      roleList: {
        page: 1,
        pagesize: 2
      },
      total: null,
      dialogVisible: false,
      row: {},
      isEdit: false,
      companyForm: { }
    }
  },
  created() {
    this.getRoleList()
    this.getCompanyList()
  },
  methods: {
    // 关闭对话框
    handleClose(done) {
      // console.log(this.$refs.set)
      this.$refs.set.clearForm()
      this.isEdit = false
      done()
    },
    handleClick(tab, event) {
      // console.log(tab, event)
    },
    // 修改
    editHandler(row) {
      console.log(row)
      this.row = row
      this.isEdit = true
      this.dialogVisible = true
    },
    // 删除
    async handleDelete(index, row) {
      this.row = row
      // console.log(index, this.row)
      const res = await deleteRole(this.row.id)
      // console.log(res)
      if (res.code === 10000) {
        this.$message.success('删除成功!')
        // console.log(this.tableData.length)
        if (this.roleList.page > 1 && this.tableData.length === 1) {
          this.roleList.page--
        }
        this.getRoleList()
      }
    },
    // 改变每页的条数
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.roleList.pagesize = val
      this.getRoleList()
    },
    // 改变当前页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.roleList.page = val
      this.getRoleList()
    },
    // 获取公司角色列表
    async getRoleList() {
      try {
        const { data } = await getRole(this.roleList)
        // console.log(data)
        this.tableData = data.rows
        this.total = data.total
      } catch (err) {
        console.log(err)
      }
    },
    // 添加角色
    addRole() {
      this.dialogVisible = true
    },
    // 成功时清除表单里的数据并关闭表单
    success() {
      if (this.tableData.length === this.roleList.pagesize) {
        this.roleList.page = this.total / this.roleList.pagesize + 1
      }
      this.dialogVisible = false
      this.isEdit = false
      this.getRoleList()
    },
    // 获取公司的信息
    async getCompanyList() {
      const res = await getCompanyInfo(this.$store.state.user.userInfo.companyId)
      // console.log(res)
      if (res.code === 10000) {
        this.companyForm = res.data
      }
    }
  }
}
</script>
